package org.example.new_hbps_backend_reconstruction.service;

import lombok.extern.slf4j.Slf4j;
import org.example.new_hbps_backend_reconstruction.entity.PatientBaseEntity;
import org.example.new_hbps_backend_reconstruction.handler.PlanTextMessageHandler;
import org.example.new_hbps_backend_reconstruction.handler.WebSocketMessageSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.socket.WebSocketSession;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author 丁利军
 * @date 2025年10月11日 21:34
 * @description TODO: 描述
 */

@Service
@Slf4j
public class WebsocketService implements PlanTextMessageHandler {

    @Autowired
    private PatientBaseService patientBaseService;

    // 存储 WebSocket 会话的映射
    private final ConcurrentHashMap<String, WebSocketSession> sessions = new ConcurrentHashMap<>();


    /**
     * 普通文本消息处理，客制化自己的对于接收消息的时的操作
     *
     * @param session 当前接收消息的session
     * @param message 文本消息
     */
    @Override
    public void handle(WebSocketSession session, String message) {
        // 业务逻辑处理
        log.info("session_id :{} ,接收到的message: {}", session.getId(), message);
        //sessions.put(session.getAttributes().get("userName").toString(), session);
    }

    public boolean sendMsg(String userName, String message) {
        try {
            WebSocketMessageSender.send(userName, message);
            return true;
        } catch (Exception e) {
            log.error("发送消息失败，用户没有建立连接");
            return false;
        }
    }

    public boolean sendMsg(Long patientUid, String message) {
        //	查表根据患者id获取患者的userName
        PatientBaseEntity tmp = patientBaseService.getById(patientUid);
        if (tmp == null) {
            log.error("患者不存在");
            return false;
        }
        String userName = tmp.getUsername();
        return sendMsg(userName, message);
    }

    // 新增方法来检查会话是否存在
    public boolean sessionExists(String userName) {
        return sessions.containsKey(userName);
    }
}

